Arrays

  • 2D Arrays

2D Arrays


There are three(3) methods of creating a two dimensional array:

int A[3][4]

First method to create an array in STACK

Visualized as as three rows and four columns:

twodim1.png

But the physical memory is linear, and we assume the integer takes 2 bytes.

A[0] = 200 to 201

A[1] = 202 to 203

A[2] = 204 to 205

A[3] = 204 to 205

.................

.................

A[22] = 220 to 221

A[23] = 222 to 223

So,

int A[3][4] = 12 bytes

12 x 2 bytes per integer = 24 bytes

Memory is allocated as a single dimensional array, but the compiler allows us to acces it as a two dimensional array, with row and column number, with TWO INDEXES

In [1]:
#include <iostream>
#include <climits>
#include <math.h>
using namespace std;
In [2]:
//Must declare and initialize at same time, if you want to use the shortened form
//int A[3][4];

How to initialize


int A[3][4]={{1,2,3,4},{4,5,3,5},{7,4,5,6}}

An the array is created in STACK

In [3]:
int A[3][4]={
    1,2,3,4,
    4,5,3,5,
    7,4,5,6
};
In [4]:
for (int i = 0; i < 3; i++){
    for (int j = 0; j < 4; j++){
        cout<<A[i][j]<<" ";
    }
   cout<<endl;
}
1 2 3 4 
4 5 3 5 
7 4 5 6 

Second method to create an array (Stack and HEAP)

Remember: We want two rows and three columns

int *A[3]

But what does this mean? This is not an array of integers, its an array of integer pointers

twodim2.png

This is an array of three pointers.

But keeping to what we need: Three 3 rows and 4 columns

Picture*

Create three array pointers in stack

This will be our rows

In [5]:
int *A1[3];

Create colums arrays in HEAP

These will be our columns

In [6]:
A1[0] = new int[4];
A1[1] = new int[4];
A1[2] = new int[4];
Out[6]:
@0x7ffd08218638
In [7]:
A1[0][0]=5;A1[0][1]=5;A1[0][2]=5;A1[0][3]=5;
A1[1][0]=6;A1[1][1]=6;A1[1][2]=6;A1[1][3]=6;
A1[2][0]=7;A1[2][1]=7;A1[2][2]=7;A1[2][3]=7;

Normally, we have a pointer in the STACK, to create (point to) an array in HEAP of certain size. Now we have an array of pointers in stack, so make up our rows. And for each row, eg. A[0] we create a new four dimensional array in heap, to present the columns.

In [8]:
for (int i = 0; i < 3; i++){
    for (int j = 0; j < 4; j++){
        cout<<A1[i][j]<<" ";
    }
   cout<<endl;
}
5 5 5 5 
6 6 6 6 
7 7 7 7 

Third method to create an array (ONLY HEAP)

twodim3.png

Remember: We want two rows and three columns

Previously the row int array pointers were in the STACK, pointing to the column pointers in the HEAP.

Now both row and column will be in the HEAP.

Through the use of double pointers. Lets explore.

int **A

We use two ** (asterisks)...to signify this A will be and array in HEAP

And A is now treated as variable, still in STACK, but will now point to rows and columns in the HEAP.

For the rows:

A=new int *[3];

For the columns:

A[0]=new int[4]; 
A[1]=new int[4]; 
A[3]=new int[4];

Setup the double pointer, to signify that A will be an array in HEAP. That is it will be redefined later on.

In [9]:
int **A2;

Create new array pointer in heap of size 3, redefining A

In [10]:
A2=new int *[3];

Create the column arrays as we did with method 2

In [11]:
A2[0]=new int[4]; 
A2[1]=new int[4]; 
A2[2]=new int[4];
In [12]:
A2[0][0]=4;A2[0][1]=4;A2[0][2]=4;A2[0][3]=4;
A2[1][0]=9;A2[1][1]=9;A2[1][2]=9;A2[1][3]=9;
A2[2][0]=3;A2[2][1]=3;A2[2][2]=3;A2[2][3]=3;
In [13]:
for (int i = 0; i < 3; i++){
    for (int j = 0; j < 4; j++){
        cout<<A2[i][j]<<" ";
    }
   cout<<endl;
}

delete []A2;
A2=nullptr;
4 4 4 4 
9 9 9 9 
3 3 3 3 
In [ ]: